// index.js
import {
  ble_send_date
} from "../../utils/util.js";
const app = getApp()
Page({
  data: {
    dateTimeStrYears: "",
    dateTimeStrTime: "",
    weekdayName: "",

    //闹钟数据
    ring_name_list: {
      "0": "星空",
      "1": "丝绸",
      "2": "轻音乐",
      "3": "钟响磬鸣‌",
      "4": "卡农",
    },
    flaValue: 0,

  },
  //设置闹钟
  onSetAlarm(e) {
    let alarm0bj = e.currentTarget.dataset.list;
    console.log(alarm0bj);
    if (alarm0bj.id == 1) {

    }

    wx.navigateTo({
      url: `../setAlarm/setAlarm?alarmObj=${JSON.stringify(alarm0bj)}`,
      routeType: 'routeType',
      success: (result) => {},
      fail: (res) => {},
      complete: (res) => {},
    })

  },

  //开关闹钟
  onChange_alarm(e) {
    let id = e.currentTarget.id;
    console.log(id);
    let {
      alarm_list
    } = app.store.getState();
    if (id == 1 || id == 2 || id == 3) {
      alarm_list[id - 1].alarm_state1 = alarm_list[id - 1].alarm_state1 ? 0 : 1;
    }
    app.store.setState({
      alarm_list
    });
    ble_send_date(id);
  },

  //开灯关灯
  onSetLedPwm() {
    let {
      bleStore
    } = app.store.getState();
    bleStore[2] ^= 1;
    app.store.setState({
      bleStore
    });
    ble_send_date(8);
  },

  //亮度调节
  onChange_pwm(event) {

    if (this.data.flaValue != event.detail.value) {
      this.data.flaValue = event.detail.value;
    } else {
      return 0;
    }
    this.setData({
      flaValue: this.data.flaValue
    })

    console.log(this.data.flaValue);
    let {
      bleStore
    } = app.store.getState();
    bleStore[3] = 100 - this.data.flaValue;
    app.store.setState({
      bleStore
    });
    ble_send_date(9);
  },

  onOpen(event) {
    console.log(event);

  },
  onClose(event) {
    console.log(event);
  },

  onClickLeft() {
    wx.redirectTo({
      url: '../logs/logs',
      routeType: 'routeType',
      success: (result) => {},
      fail: (res) => {},
      complete: (res) => {},
    })

  },

  //返回搜索界面
  onOpenLogs() {
    let { bleUser } = app.store.getState();
    let deviceId = bleUser.deviceId;
    if (deviceId) {
      wx.closeBLEConnection({
        deviceId,
        success (res) {
          console.log(res)
        }
      })
    }
    wx.reLaunch({
      url: '../logs/logs'
    })
  },

  weekday(year, month, day) {
    if (month < 3) {
      month += 12;
      year--;
    }
    let w = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
    return w;
  },


  getDateTime(that) {
    //创建时间，日期，对象
    var date = new Date();
    //获取年月日时分秒
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var i = date.getMinutes();
    var s = date.getSeconds();
    //如果个位前面补充零
    m = m < 10 ? '0' + m : m;
    d = d < 10 ? '0' + d : d;
    h = h < 10 ? '0' + h : h;
    i = i < 10 ? '0' + i : i;
    s = s < 10 ? '0' + s : s;
    this.setData({
      dateTimeStrYears: y + '-' + m + '-' + d
    });
    this.setData({
      dateTimeStrTime: h + ':' + i
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //正确的示范
    // let { user } = app.store.getState();
    // user.name = '张三';
    // app.store.setState({
    //   user
    // });
    console.log(this.data.$state); // 这是一个全局状态

    this.getDateTime();
    setInterval(this.getDateTime.bind(this), 1000);
    //开启定时器

    //显示星期
    var date = new Date();
    let list = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日", ]
    let n = this.weekday(date.getFullYear(), date.getMonth() + 1, date.getDate())
    this.setData({
      weekdayName: list[Math.floor(n)]
    })

  },


  onUnload: function () {
    wx.offAccelerometerChange()
  },

})